package cn.sxt.controller;

import org.springframework.web.bind.annotation.RequestMapping;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;

import cn.sxt.domain.Reply;
import cn.sxt.domain.Topic;
import cn.sxt.service.ReplyService;
import cn.sxt.service.TopicService;
import cn.sxt.vo.ReplyVo;
import cn.sxt.vo.TopicVo;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lij
 * @since 2018-10-08
 */
@Controller
@RequestMapping("/reply")
public class ReplyController {
	
	@Autowired
	private ReplyService replyService;
	
	@Autowired
	private TopicService topicService;
	
	@RequestMapping("queryReply")
	public String queryAll(Model model,TopicVo topicVo,HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		
		//查当前帖子
		Topic topic = topicService.selectById(topicVo.getTopicid());
		topic.setClickamount(topic.getClickamount()+1);
		
		topicService.updateById(topic);
		Wrapper<Reply> wrapper = new EntityWrapper<>();
		
		wrapper.eq("topicid", topicVo.getTopicid());
		List<Reply> list = replyService.selectList(wrapper);
		model.addAttribute("topic", topic);
		model.addAttribute("list", list);
		/*request.setAttribute("list", list);
		request.getRequestDispatcher("../relist.jsp").forward(request, response);*/
		return "../relist.jsp";
	}
	
	@RequestMapping("addReply")
	public String addReply(ReplyVo replyVo) {
		replyVo.setCreatedate(new Date());
		replyService.insert(replyVo);
		return "queryReply.action";
	}
}
